"use strict";
var Premium = Premium || {};

/* syncMedia ------------------------------------------------------------------------- */
/**
 * @desc Syncs one media element to another.
 * @param {HTMLMediaElement} mediaMaster The master, controlling video 
 * @param {string} mediaSub The suboridinate, controlled video 
 */
const syncMedia = function(mediaMaster, mediaSub) {
    mediaMaster.addEventListener("playing", function() {
        mediaSub.currentTime = mediaMaster.currentTime;
        mediaSub.play().catch((e) => {});
    })
    mediaMaster.addEventListener("pause", function() {
        mediaSub.pause();
    })
    mediaMaster.addEventListener("volumechange", function() {
        mediaSub.muted = mediaMaster.muted;
        if (!mediaMaster.paused){
            mediaSub.play().catch((e) => {});
            mediaSub.currentTime = mediaMaster.currentTime;
        } 
    })
};
/* syncMedia END --------------------------------------------------------------------- */

/* videoTempPause -------------------------------------------------------------------- */
/**
 * @desc Pauses video for 500 milliseconds. (Helps with smoothness and speed of expanding)
 * @param {HTMLVideoElement} [video] - target video
 */
const videoTempPause = function(video) {
    video = video || document.querySelector("VIDEO");
    video.pause();
    clearTimeout(video.pauseRef);
    video.pauseRef = setTimeout(function() {
        if (PremiumJpControls.getLastAction(video.parentElement).playPause !== "pause") {
            video.play().catch((e) => {});
        }
    }, 500)
};
/* videoTempPause END ---------------------------------------------------------------- */

/* safariFix ------------------------------------------------------------------------- */
/**
 * @desc Applies the 'Safari fix' to prevent needing to click again to replay the video
 * @param {HTMLVideoElement} [video] - target video
 */
const safariFix = function(video) {
    document.body.addEventListener("click", function(e) {
        if (e.target.className && (e.target.className.indexOf("jp-controls-play") > -1 || e.target.className.indexOf("jp-controls-bigplay") > -1)) {
            if (!video.paused) {
                setTimeout(function() {
                    video.play();
                }, 500)
            }
        }
    });
}
/* safariFix END --------------------------------------------------------------------- */

Premium.creative = {
    init: function() {

        /*********************************************************************
         * Premium.addon.mobileexpandable.init(height, expandedHeight, swipeHeight, expandedSwipeHeight);
         * @desc Converts mobile footer to mobile expandable
         * @param {string} [height="25%"] - height of default panel
         * @param {string} [expandedHeight="100%"] - height of expanded panel
         * @param {number} [swipeHeight=1] - height of swipe area on default panel
         * @param {number} [expandedSwipeHeight=1] - height of swipe area on expanded panel
         *********************************************************************/
        Premium.addon.mobileexpandable.init("25%", "58%", 1, 1);

        Premium.product.initOrientationMessage();
        /* START OF CUSTOM JS */

        switch (document.body.id) {
            case "body_main":
                Premium.communicator.api.receiveMessage(function(mess) {
                    switch (mess.action) {
                        case "mfex-expand":
                            // expand code here
                            // videoTempPause();
                            break;

                        case "mfex-default":
                            // default/collapse code here
                            // videoTempPause();
                            break;

                        case "mfex-expand-complete":
                            // expand complete code here
                            break;

                        case "mfex-default-complete":
                            // default complete code here
                            break;
                    }
                })

                Premium.creative.createVideoPlayerAppended = function(video) {

                    // -------------------------------------------------
                    // var audioEl = document.querySelector("AUDIO");
                    // syncMedia(video, audioEl);
                    // Premium.video.initPlayFromUser(video, audioEl);
                    // Premium.product.onBlurChange(function(isHidden) {
                    //     audioEl.muted = video.muted;
                    // })
                    // -------------------------------------------------
                    // OR
                    // -------------------------------------------------
                    Premium.video.initPlayFromUser(video);
                    // -------------------------------------------------

                    Premium.video.sync(video, 1);
                    video.play().catch((e) => {});
                    safariFix(video);
                }
                break;

            case "body_expanded":
                Premium.communicator.api.receiveMessage(function(mess) {
                    switch (mess.action) {
                        case "mfex-expand":
                            // expand code here
                            gsap.to(".content-wrapper", .5, { delay: .5, autoAlpha: 1 });
                            break;

                        case "mfex-default":
                            // default/collapse code here
                            gsap.to(".content-wrapper", .5, { autoAlpha: 0 });
                            break;

                        case "mfex-expand-complete":
                            // expand complete code here
                            break;

                        case "mfex-default-complete":
                            // default complete code here
                            break;
                    }
                })

                var videoEl = document.querySelector("VIDEO");
                Premium.video.sync(videoEl, undefined, Premium.video.SyncType_Get);
                // videoEl.volume = "0";
                videoEl.play().catch((e) => {});
                safariFix(videoEl);
                break;

        }

        /* END OF CUSTOM JS */
    }
}